package server.commonData;

import java.util.Map;

import server.ScriptEnum;
import server.ServerEngin;
import server.business.task.data.ITaskEventScript;
import server.business.task.data.TaskList;
import server.commonData.interfaces.IByteRead;
import server.commonData.interfaces.IByteWrite;
import server.commonData.interfaces.IUpdate;

import com.core.NetChannel;

/**
 * 客户端玩家
 * @author King
 *
 */
public class User extends Player implements IByteRead,IByteWrite ,IUpdate
{
	/**网络层**/
	protected NetChannel channel;
	
	/**任务列表**/
	protected TaskList taskList = new TaskList();
	
	/**帐号**/
	protected String account;
	
	/**是否禁言*/
	protected boolean shutup;
	
	/**是否禁止登陆**/
	protected boolean freeze;
	
	public User(NetChannel channel)
	{
		super();
		this.channel = channel;
		channel.setCahceInfo(this);
		this.type = USER;
		taskList.init();
	}
	
	@Override
	public void readFromDb(Map<String, Object> map)
	{
		this.id = Utils.readLong(map, "id");
		this.account = Utils.readString(map, "account");
		this.money = Utils.readInt(map, "money");
		this.face = Utils.readInt(map, "face");
		this.yuanbao=Utils.readInt(map, "yuanbao");
		this.vipLevel = Utils.readInt(map, "vipLevel");
		this.vipEndTime = System.currentTimeMillis()+Utils.readInt(map, "vipTimeLeft");
		this.name = Utils.readString(map, "name");
		this.winNum = Utils.readInt(map, "winNum");
		this.failNum = Utils.readInt(map, "failNum");
		this.rechargedGold = Utils.readInt(map, "rechargedGold");
		this.level = Utils.readInt(map, "level");
		this.meili = Utils.readInt(map, "meili");
		this.shutup = Utils.readBool(map, "shutup");
		this.freeze = Utils.readBool(map, "freeze");
		//任务读库
		String task  = Utils.readString(map, "task");
		taskList.fromDb(task);
	}

	public NetChannel getChannel()
	{
		return channel;
	}
	
	
	public void setChannel(NetChannel channel) {
		this.channel = channel;
		channel.setCahceInfo(this);
	}

	@Override
	public void writeToDb(Map<String, Object> map)
	{
		map.put("id", this.id);
		map.put("money", this.money);
		map.put("face", this.face);
		map.put("yuanbao", this.yuanbao);
		map.put("vipLevel", this.vipLevel);
		int vipTimeLeft = (int)(this.vipEndTime-System.currentTimeMillis());
		if(vipTimeLeft<0)
		{
			vipTimeLeft = 0;
		}
		map.put("vipTimeLeft", vipTimeLeft);
		map.put("name", this.name);
		map.put("winNum", this.winNum);
		map.put("failNum", this.failNum);
		map.put("rechargedGold", this.rechargedGold);
		map.put("level", this.level);
		map.put("meili", this.meili);
		map.put("freeze", freeze?1:0);
		map.put("shutup", shutup?1:0);
		//任务存库
		map.put("task", this.taskList.toDb());
	}

	public TaskList getTaskList()
	{
		return taskList;
	}

	@Override
	public void onTime(long curMill)
	{
		ITaskEventScript script = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.TASK_EVENT);
		script.onUpdate(this, curMill);
	}

	public String getAccount() {
		return account;
	}

	public boolean isShutup() {
		return shutup;
	}

	public void setShutup(boolean shutup) {
		this.shutup = shutup;
	}

	public boolean isFreeze() {
		return freeze;
	}

	public void setFreeze(boolean freeze) {
		this.freeze = freeze;
	}

	public void setTaskList(TaskList taskList) {
		this.taskList = taskList;
	}

	public void setAccount(String account) {
		this.account = account;
	}
}
